package com.e_library.dao.classes;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.e_library.jpa.entities.Main;
import com.e_library.jpa.entities.Rayon;



public class RayonDaoImpl implements RayonIDao{

	private EntityManager em;
    public RayonDaoImpl() {
    	Main m=new Main();
          em=m.em;
          em.getTransaction().begin();
	}
	
	 public void deleteOne(String id) {
		 Rayon  rayon= em.find(Rayon.class, id);
	 if (rayon == null) {
	throw new DaoException(2);
	}
	em.remove(rayon);
	 em.getTransaction().commit();
	 }
	
	@SuppressWarnings("unchecked")
   public List<Rayon> getAll() {
	 return em.createQuery("select p from Rayon p").getResultList();
	 }
	
	 @SuppressWarnings("unchecked")
	 public List<Rayon> getAllLike(String modele) {
	 return em.createQuery("select p from Rayon p where p.nom_rayon like :modele")
	 .setParameter("modele", modele).getResultList();
	 }
	
	 public Rayon getOne(String id) {
	 return em.find(Rayon.class, id);
	 }
	

	 public Rayon saveOne(Rayon rayon) {
	 em.persist(rayon);
	 em.getTransaction().commit();
	 return rayon;
 }
	
	 
	 public Rayon updateOne(Rayon rayon) {
		 Rayon rayon2=em.merge(rayon);
		 em.getTransaction().commit();
	 return rayon2;
	 }
}
